package com.lovely602.langchain4j.param.listener;


import cn.hutool.core.util.IdUtil;
import dev.langchain4j.model.chat.listener.ChatModelErrorContext;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.listener.ChatModelRequestContext;
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import lombok.extern.slf4j.Slf4j;

/**
 * 通义千问聊天模型监听器
 *
 * @author lizhixing
 */
@Slf4j
public class QWenChatModelListener implements ChatModelListener {


    @Override
    public void onRequest(ChatModelRequestContext requestContext) {

        String traceId = IdUtil.simpleUUID();
        log.info("请求Qwen模型开始,traceId:{}", traceId);

        requestContext.attributes().put("traceId", traceId);
        ChatModelListener.super.onRequest(requestContext);
    }


    @Override
    public void onResponse(ChatModelResponseContext responseContext) {

        log.info("请求Qwen模型结束,traceId:{}", responseContext.attributes().get("traceId"));
        ChatModelListener.super.onResponse(responseContext);
    }

    @Override
    public void onError(ChatModelErrorContext errorContext) {

        log.error("请求Qwen模型异常", errorContext.error());
        ChatModelListener.super.onError(errorContext);
    }
}
